package com.jn.gsondemo.views;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.jn.gsondemo.R;
import com.jn.gsondemo.base.BaseActivity;
import com.jn.gsondemo.entity.Book;
import com.jn.gsondemo.entity.HotSearch;
import com.jn.gsondemo.entity.Olympic;
import com.jn.gsondemo.utils.FileUtils;

import java.util.List;

public class HotsearchActivity extends BaseActivity {

    private List<HotSearch.Result> result;

    @Override
    public int getLayoutId() {
        return R.layout.activity_hotsearch;
    }

    @Override
    public void init() {
        findViewById(R.id.iv_left_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        String fromAssets = FileUtils.getFromAssets(this, "hotsearch.json");
        System.out.println(fromAssets);
        Gson gson = new Gson();
        HotSearch hotSearch = gson.fromJson(fromAssets, HotSearch.class);
        System.out.println("===========" + hotSearch.code);
        result = hotSearch.result;
        RecyclerView recyclerview = findViewById(R.id.recyclerview);

        MyAdapter mMyAdapter = new MyAdapter();
        recyclerview.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(HotsearchActivity.this);
        recyclerview.setLayoutManager(layoutManager);
    }


    public class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {

        //绑定item布局
        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(HotsearchActivity.this, R.layout.item_layout, null);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        //给item添加数据
        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            HotSearch.Result result1 = result.get(position);
            holder.title.setText("标题：" + result1.title);
            holder.content.setText(" 排名：" + result1.index + " 阅读量：" + result1.hotValue);
            holder.layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(HotsearchActivity.this, result1.title, Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(HotsearchActivity.this, WebActivity.class);
                    intent.putExtra("title", result1.title);
                    intent.putExtra("link", result1.link);
                    startActivity(intent);
                }
            });
        }

        //设置item条数
        @Override
        public int getItemCount() {
            return result == null ? 0 : result.size();
        }
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView title;
        TextView content;
        ConstraintLayout layout;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            layout = itemView.findViewById(R.id.layout);
            title = itemView.findViewById(R.id.textView2);
            content = itemView.findViewById(R.id.textView3);
        }
    }
}